//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.indexes.genshanguserList;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.EntryTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.TableShell;
import com.nebutown.commonunits.dataface.definitions.ButtonField;
import com.nebutown.commonunits.dataface.definitions.FieldValue;
import com.nebutown.commonunits.dataface.definitions.PageInfo;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.genshanguser.GenShangUser;
import com.restaurant.application.genshanguser.support.GenShangUserData;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListBean;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListCriteria;
import com.restaurant.application.indexes.genshanguserList.mysql.GenShangUserListImpl;
import com.restaurant.application.indexes.genshanguserList.support.AbstractAssistGenShangUserList;
import com.restaurant.application.indexes.genshanguserList.support.SearchData;
import com.restaurant.application.util.CommonUtils;
import java.util.List;
import java.util.Objects;

public class AssistGenShangUserListImpl extends AbstractAssistGenShangUserList {
    public AssistGenShangUserListImpl() {
    }

    public ViewUpdate search(SearchData search, int[] seq) {
        return this.returnView(new View[]{this.newCommonTable(search)});
    }

    private String getWhere(SearchData search) {
        StringBuilder builder = new StringBuilder();
        builder.append(" true");
        if (!Strings.isNullOrEmpty(search.getName())) {
            builder.append(" and NICKNAME like  '%").append(search.getName()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getNumber())) {
            builder.append(" and NUMBER like '%").append(search.getNumber()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getPhone())) {
            builder.append(" and PHONE like '%").append(search.getPhone()).append("%'");
        }

        builder.append(" ORDER BY MODIFYTIME DESC, RECORD_ID ASC");
        return builder.toString();
    }

    public ViewUpdate create(int[] seq) {
        UID uid = this.createNode(GenShangUser.class.getName()).getId();
        return this.returnPushInContainer(uid, false);
    }

    public ViewUpdate modify(int[] seq) {
        List<GenShangUserListBean> beans = ((GenShangUserListImpl)this.getIndex(GenShangUserListImpl.class)).search((new GenShangUserListCriteria()).withRecordId((long)seq[0]), (SortSpec)null, 1);
        return beans.size() > 0 ? this.returnPushInContainer(((GenShangUserListBean)beans.get(0)).getUserUid(), false) : this.returnView(new View[]{this.getFullView()});
    }

    public View getFullView() {
        return this.newCommonTable(new SearchData());
    }

    public View getViewLead(ViewSpec spec) {
        return null;
    }

    public GenShangUserListBean createBean(UID uid, GenShangUserData data) {
        if (!Objects.isNull(uid) && !Objects.isNull(data)) {
            String roleNameList = null;
            if (Objects.nonNull(data.getRoleNameList()) && data.getRoleNameList().size() > 0) {
                roleNameList = String.join(",", data.getRoleNameList());
            }

            return (new GenShangUserListBean()).withUserUid(uid).withId(data.getId()).withNumber(data.getNumber()).withCreateTime(data.getCreateTime()).withModifyTime(data.getModifyTime()).withAvatarUrl(data.getAvatarUrl()).withNickName(data.getNickName()).withRealName(data.getRealName()).withGender(data.getGender()).withPassword(data.getPassword()).withPhone(data.getPhone()).withShopName(data.getShopName()).withShopUid(Objects.nonNull(data.getShopUid()) ? data.getShopUid().value() : null).withState(data.getState()).withRoleNameList(roleNameList).withAdminOrNot(String.valueOf(data.getAdminOrNot()));
        } else {
            return null;
        }
    }

    public GenShangUserListBean updateBean(UID uid, GenShangUserData data, GenShangUserListBean bean) {
        if (!Objects.isNull(uid) && !Objects.isNull(data) && !Objects.isNull(bean)) {
            bean.withUserUid(uid);
            if (Objects.nonNull(data.getModifyTime())) {
                bean.withModifyTime(data.getModifyTime());
            }

            if (!Strings.isNullOrEmpty(data.getNickName())) {
                bean.withNickName(data.getNickName());
            }

            if (!Strings.isNullOrEmpty(data.getAvatarUrl())) {
                bean.withAvatarUrl(data.getAvatarUrl());
            }

            if (!Strings.isNullOrEmpty(data.getRealName())) {
                bean.withRealName(data.getRealName());
            }

            if (!Strings.isNullOrEmpty(data.getState())) {
                bean.withState(data.getState());
            }

            if (!Strings.isNullOrEmpty(data.getPassword())) {
                bean.withPassword(data.getPassword());
            }

            if (!Strings.isNullOrEmpty(data.getGender())) {
                bean.withGender(data.getGender());
            }

            if (!Strings.isNullOrEmpty(data.getPhone())) {
                bean.withPhone(data.getPhone());
            }

            if (!Strings.isNullOrEmpty(data.getShopName())) {
                bean.withShopName(data.getShopName()).withShopUid(data.getShopUid().value());
            }

            if (Objects.nonNull(data.getRoleNameList()) && data.getRoleNameList().size() > 0) {
                bean.withRoleNameList(String.join(",", data.getRoleNameList()));
            }

            return bean;
        } else {
            return null;
        }
    }

    private View newCommonTable(SearchData search) {
        int page = 1;
        if (Objects.nonNull(search.getPage()) && search.getPage() > 1) {
            page = search.getPage();
        }

        String where = this.getWhere(search);
        List<GenShangUserListBean> beans = ((GenShangUserListImpl)this.getIndex(GenShangUserListImpl.class)).search(where, (page - 1) * 10, page * 10);
        int totalRecord = ((GenShangUserListImpl)this.getIndex(GenShangUserListImpl.class)).count(where);
        PageInfo info = CommonUtils.getPageInfo(page, 10, totalRecord, this, CMD_SEARCH, "page");
        return ((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(this.unitPosition(), this.newFormShell(search, beans, info).getForm());
    }

    private FormShell newFormShell(SearchData search, List<GenShangUserListBean> beans, PageInfo info) {
        FormShell shell = new FormShell("用户管理/用户信息", 3, 3);
        shell.source(search, DataModes.EDIT);
        shell.at(0, 0, "用户名称").stringField("name", search.getName()).placeHolder("用户名称").setEditable(true);
        shell.at(0, 1, "用户编号").stringField("number", search.getNumber()).placeHolder("用户编号").setEditable(true);
        shell.at(0, 2, "手机号").stringField("phone", search.getPhone()).placeHolder("手机号").setEditable(true);
        List<ButtonField> buttons = Lists.newArrayList();
        buttons.add(new ButtonField(this.getPath(CMD_SEARCH, new int[0]), "查询", true, "", false));
        if (!Strings.isNullOrEmpty(this.getPath(CMD_CREATE, new int[0]))) {
            buttons.add(new ButtonField(this.getPath(CMD_CREATE, new int[0]), "新增", true, "", false));
        }

        shell.at(1, 0, (String)null).buttonBoxField((ButtonField[])buttons.toArray(new ButtonField[0]));
        TableShell<GenShangUserListBean> tableShell = new TableShell(GenShangUserListBean.class, beans);
        tableShell.createColumn("modifyTime").withName("时间").withWidth(180).cellCalculator((e) -> {
            GenShangUserListBean bean = (GenShangUserListBean)e;
            return (new FieldValue()).withValue(DateTimeUtils.toTime(bean.getModifyTime()));
        });
        tableShell.createColumn("id").withName("用户编号").withWidth(165).cellCalculator((e) -> {
            GenShangUserListBean bean = (GenShangUserListBean)e;
            return (new FieldValue()).withValue(bean.getId());
        });
        tableShell.createColumn("nickName").withName("登录名").withWidth(220).cellCalculator((e) -> {
            GenShangUserListBean bean = (GenShangUserListBean)e;
            return (new FieldValue()).withValue(bean.getNickName());
        });
        tableShell.createColumn("realName").withName("用户姓名").withWidth(220).cellCalculator((e) -> {
            GenShangUserListBean bean = (GenShangUserListBean)e;
            return (new FieldValue()).withValue(bean.getRealName());
        });
        tableShell.createColumn("gender").withName("性别").withWidth(75).cellCalculator((e) -> {
            GenShangUserListBean bean = (GenShangUserListBean)e;
            return (new FieldValue()).withValue(bean.getGender());
        });
        tableShell.createColumn("phone").withName("手机号").withWidth(120).cellCalculator((e) -> {
            GenShangUserListBean bean = (GenShangUserListBean)e;
            return (new FieldValue()).withValue(bean.getPhone());
        });
        tableShell.createColumn("roleNameList").withName("角色").withWidth(160).cellCalculator((e) -> {
            GenShangUserListBean bean = (GenShangUserListBean)e;
            return (new FieldValue()).withValue(bean.getRoleNameList());
        });
        tableShell.createColumn("state").withName("用户状态").withWidth(90).cellCalculator((e) -> {
            GenShangUserListBean bean = (GenShangUserListBean)e;
            return (new FieldValue()).withValue(bean.getState());
        });
        tableShell.createColumn("removeLine").withName("操作").withWidth(100).cellCalculator((e) -> {
            GenShangUserListBean bean = (GenShangUserListBean)e;
            List<ButtonField> fields = Lists.newArrayList();
            if (!Strings.isNullOrEmpty(this.getPath(CMD_MODIFY, new int[0]))) {
                fields.add((new ButtonField()).withLabel("编辑").withWithData(false).withButtonPath(this.getPath(CMD_MODIFY, new long[]{bean.getRecordId()})));
            }

            return fields.size() > 0 ? (new FieldValue()).withButtonBox(fields).withEntryType(EntryTypes.BUTTONBOX) : (new FieldValue()).withValue("");
        });
        tableShell.withPageInfo(info);
        shell.at(2, 0, "用户管理/用户信息").large(3, 1).tableField("userList", tableShell);
        return shell;
    }
}
